package com.zhq.tool.util;

import com.alibaba.fastjson.JSON;
import com.zhq.tool.db.LocalStorage;

import java.io.*;
import java.nio.charset.StandardCharsets;

/**
 * @ProjectName: YcToolSwing
 * @Package: com.yc.tool.util
 * @ClassName: FileUtils
 * @Author: jacky
 * @Description: 保存文件
 * @Date: 2022/2/15 9:25
 * @Version: 1.0
 */
public class FileUtils {

    public static LocalStorage getLocalStorage() {
        String path = FileUtils.class.getProtectionDomain().getCodeSource().getLocation().getPath();
        String localCatalogue = path.replaceAll("target/classes/", "")
                .replaceAll("FdTool.jar", "")
                .trim();
        String detailPath = localCatalogue + "localStorage.db";
        File file = new File(detailPath);
        LocalStorage localStorage = new LocalStorage();
        localStorage.setPath(detailPath);
        try {

            if (file.exists()) {
                FileReader fileReader = new FileReader(file);
                Reader reader = new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8);
                int ch = 0;
                StringBuffer sb = new StringBuffer();
                while ((ch = reader.read()) != -1) {
                    sb.append((char) ch);
                }
                fileReader.close();
                reader.close();
                String json = sb.toString();
                if (StringUtils.isEmpty(json)) {
                    return localStorage;
                } else {
                    localStorage = JSON.parseObject(json, LocalStorage.class);
                    return localStorage;
                }
            } else {
                if (!file.getParentFile().exists()) {
                    file.getParentFile().mkdirs();
                }
                if (!file.exists()) {
                    file.createNewFile();
                }
                return localStorage;
            }
        } catch (IOException e) {
            return localStorage;
        }
    }

    public static void main(String[] args) {
    }

    public static void saveFile(String path, String data) {
        File file = new File(path);
        BufferedWriter bufferedWriter = null;
        try {
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }
            if (!file.exists()) {
                file.createNewFile();
            } else {
                file.delete();
                file.createNewFile();
            }
            bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
            bufferedWriter.write(data);
        } catch (IOException e) {
            System.out.println("e:" + e.getLocalizedMessage());
        } finally {
            try {
                bufferedWriter.flush();
                bufferedWriter.close();
            } catch (IOException ex) {
                System.out.println("e:" + ex.getLocalizedMessage());
            } catch (NullPointerException ex) {
                System.out.println("文件创建失败，路径不对或无读写权限！ 错误编码：" + ex.getLocalizedMessage());
            }
        }
        System.out.println("保存文件成功！！！");
    }

}
